<?php
/**
 * 栏目管理
 *
 * PHP Version 5.6
 *
 * @author    guanxu <510104561@qq.com>
 * @time      2017/08/11
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Common\ConstantUtil;
Class ClassifyController extends CommonController {

    public function index() {
        $list =  M("classify")->where(["status"=>ConstantUtil::GLOBAL_TRUE,"classify"=>ConstantUtil::SYSTEM_ADMIN])->select();
        foreach($list as $key => $val) {
            $list[$key]["sys_column"] = M("sys_column")->where(["id"=>$val["sys_column_id"]])->getField("name");
        }
        $this->assign("list",$list);
        $this->display("classify/index");
    }


    public function add() {
        $name = I("post.name");
        $sys_column_id          = I("post.sys_column");
        $data["name"]           = $name;
        $data["status"]         = ConstantUtil::GLOBAL_TRUE;
        $data["create_time"]    = date("Y-m-d H:i:s");
        $data["classify"]       = ConstantUtil::SYSTEM_ADMIN;
        $data["sys_column_id"]  = $sys_column_id;
        $result = M("classify")->add($data);
        A("index")->header();
        if(empty($result)) {
            $this->error("添加失败!");
        } else {
            $this->success("添加成功!");
        }
    }

    public function addView() {
        $list = M("sys_column")->where(["status"=>ConstantUtil::GLOBAL_TRUE])->select();
        $this->assign("list",$list);
        $this->display("classify/add");
    }
    

    public function editorView() {
        $id = I("get.id");
        if(empty($id)) return '';
        $sysColumn = M("sys_column")->where(["status"=>ConstantUtil::GLOBAL_TRUE])->select();
        $list = M("classify")->where(["id"=>$id])->find();
        $this->assign(["list"=>$list,"sysColumn"=>$sysColumn]);
        $this->display("classify/editor");
    }
    public function editor() {

        $id = I("post.id");
        $name = I("post.name");
        $sys_column_id = I("post.sys_column");
        if(empty($id)) return '';
        M("classify")->where(["id"=>$id])->save(["name"=>$name,"sys_column_id"=>$sys_column_id]);
        A("index")->header();
        $this->success("添加成功!");
    }

    /**
     * 删除
     * @return string
     */

    public function delete() {
        $id = I("get.id");
        if(empty($id)) return '';
        M("classify")->where(["id"=>$id])->save(["status"=>ConstantUtil::GLOBAL_FALSE]);
        $modules_id = M("modules")->where(["classify_id"=>$id])->getField("id",true);
        D("modules")->upDelete($modules_id);
        $this->success("删除成功!");
    }

}